<?php
/**
 * 功能: 登录
 * User: 文丹
 * Date: 2018/5/29 0029
 * Time: 11:52
 */

namespace app\admin\controller;
use think\captcha\Captcha;
use think\Controller;
use think\Db;

class Login extends Controller{
    /**
     * 登录页面
     */
    public function index(){
        return $this->fetch('index:login');
    }
    /**
     * 判断用户是否登录成功  账号密码是否成功匹配
     */
    public function loginEnter1()
    {
        $captcha = new Captcha();
        $name = trim(input('account'));
        //var_dump($name);die;
        $password = trim(input('password'));
        $coke = trim(input('code'));
        $online = intval(input("online"));
        //  var_dump($online) ;exit;
       // echo 1;
        $repass = Db::name('manager')->where('account', 'admin')->where('admin_del', 0)->fetchSql()->value('password');
        dump($repass) ;
        die;
        if (empty($name) || empty($password) || empty($coke)) {
            return json(['msg'=>'请填写完整的信息！','ok'=>false]);
        }
        if (!empty($name) && !empty($password)&& !empty($coke)) {
             if (!$captcha->check($coke)) {
                 return json(['msg'=>'验证码错误！','ok'=>false]);
             }



        }
    }

    /**
     * 登录
     */
    public function loginEnter()
    {
        $captcha = new Captcha();
        $name = trim(input('account'));
        //var_dump($name);die;
        $password = trim(input('password'));
        $coke = trim(input('code'));
        $online = intval(input("online"));
        //  var_dump($online) ;exit;
        if (empty($name) || empty($password) || empty($coke)) {
            return json(['msg'=>'请填写完整的信息！','ok'=>false]);
        }
        if (!empty($name) && !empty($password)&& !empty($coke)) {
            if (!$captcha->check($coke)) {
                return json(['msg'=>'验证码错误！','ok'=>false]);
            }

            $res_static = db('manager')->where('account', $name)->value('is_able');
            if($res_static == 0){
                return json(['msg'=>'你的账户已经被禁封！请联系网站管理员！','ok'=>false]);

            }
           $repass = db('manager')->where('account', $name)->where('admin_del', 0)->value('password');

            $id = db('manager')->where('account', $name)->value('id');
            if ($repass == md5($password)) {
                session('name', $name);
                session('managerId', $id);
                if ($online == 1) {
                    setcookie("managerId", $id, time() + 3600 * 24);
                    setcookie("name", $name, time() + 3600 * 24);
                } elseif ($online == 0) {
                    setcookie("adminId", $id, time() + 3600);
                    setcookie("name", $name, time() + 3600);
                }
                return json(['msg'=>'登陆成功！','ok'=>true]);
            } else {
                return json(['msg'=>'用户名或密码错误！','ok'=>false]);
            }

        }
    }
    /**
     * 退出
     */
    public function logout(){
        session('name', null);
        session('managerId', null);

        cookie("managerId", null);
        cookie("name", null);
        $this->success('已退出', "admin/login/index");
    }


}